Elementwise Minimum
This is an example of computing the elementwise minimum from multiple input tensors of the same size.
from csdl_om import Simulatorfrom csdl import Modelimport csdlimport numpy as np
class ExampleElementwise(Model):
def define(self):
m = 2 n = 3 # Shape of the three tensors is (2,3) shape = (m, n)
# Creating the values for two tensors val1 = np.array([[1, 5, -8], [10, -3, -5]]) val2 = np.array([[2, 6, 9], [-1, 2, 4]])
# Declaring the two input tensors tensor1 = self.declare_variable('tensor1', val=val1) tensor2 = self.declare_variable('tensor2', val=val2)
# Creating the output for matrix multiplication ma = self.register_output('ElementwiseMin', csdl.min(tensor1, tensor2)) assert ma.shape == (2, 3)
sim = Simulator(ExampleElementwise())sim.run()
print('tensor1', sim['tensor1'].shape)print(sim['tensor1'])print('tensor2', sim['tensor2'].shape)print(sim['tensor2'])print('ElementwiseMin', sim['ElementwiseMin'].shape)print(sim['ElementwiseMin'])
[[ 1. 5. -8.] [10. -3. -5.]]tensor2 (2, 3)[[ 2. 6. 9.] [-1. 2. 4.]]ElementwiseMin (2, 3)[[ 1. 5. -8.] [-1. -3. -5.]]